package com.yubest.demo.anno;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.yubest.demo.service.JsonStrategy;
import com.yubest.demo.service.impl.*;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.lang.annotation.*;

/**
 * @Author hweiyu
 * @Description
 * @Date 2021/3/10 17:04
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
@Inherited
@JacksonAnnotationsInside
@JsonSerialize(using = MyJsonSerializer.class)
public @interface JsonField {

    Strategy strategy() default Strategy.NON;

    @Getter
    @AllArgsConstructor
    enum Strategy {

        NON(NonJsonStrategy.class),

        HIDE(HideJsonStrategy.class),

        LIMIT(LimitJsonStrategy.class),

        TO_BASE64(ToBase64JsonStrategy.class),

        ;

        private Class<? extends JsonStrategy> clz;

        public JsonStrategy newStrategy() {
            try {
                return clz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
